import request from '@/utils/request'
import { MenuResponseData, Role, RoleResponseData } from './type'

enum API {
  // 获取全部职位的接口地址
  ALL_ROLE_URL = '/admin/acl/role/',
  // 新增一个职位的接口地址
  ADD_ROLE_URL = '/admin/acl/role/save',
  // 修改职位的接口地址
  UPDATE_ROLE_URL = '/admin/acl/role/update',
  // 获取权限菜单和已选权限的接口地址
  ALL_PERMISSION_URL = '/admin/acl/permission/toAssign/',
  // 为职位分配权限的接口地址
  SET_PERMISSION_URL = '/admin/acl/permission/doAssign',
  // 删除职位的接口地址
  DELETE_ROLE_URL = '/admin/acl/role/remove/',
}

// 获取全部职位的接口方法
export const reqAllRole = (page: number, limit: number, keyWord: string) =>
  request.get<any, RoleResponseData>(
    API.ALL_ROLE_URL + `${page}/${limit}?roleName=${keyWord}`,
  )
// 新增/修改职位的接口方法
export const reqAddOrUpdateRole = (data: Role) =>
  data.id
    ? request.put<any, any>(API.UPDATE_ROLE_URL, data)
    : request.post<any, any>(API.ADD_ROLE_URL, data)
// 获取权限菜单和已选权限的接口方法
export const reqAllMenuList = (roleId: number) =>
  request.get<any, MenuResponseData>(API.ALL_PERMISSION_URL + roleId)
// 为职位分配权限的接口方法
export const reqSetPermission = (roleId: number, permissionID: number[]) =>
  request.post<any, any>(
    API.SET_PERMISSION_URL + `?roleId=${roleId}&permissionId=${permissionID}`,
  )
// 删除已有职位的接口方法
export const reqRemoveRole = (roleId: number) =>
  request.delete<any, any>(API.DELETE_ROLE_URL + roleId)
